﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using WP.IM.DataEntity;
using WP.IM.DataAccess;
using WP.IM.BusinessRule;
using WP.DB;
using WP.Commons;

namespace WP.IM.BusinessLogical
{
    public class ImSemiProductBLL : IBusinessLogical
    {
        ImSemiProductDAL imInvMasterDAL = new ImSemiProductDAL();

         public ImSemiProductBLL()
         {
         }

        #region For implement interface

        #region Select Record by RECID OverLoad +1
         public ImSemiProductDEL SelectDataByRecID(DBAccess dBAccess, string strRecId)
         {
             ImSemiProductDEL imInvMasterDEL = new ImSemiProductDEL();

             DataFunctions.SelectByRecID(dBAccess, imInvMasterDEL, imInvMasterDAL.Fields, strRecId);

             return imInvMasterDEL;
         }

        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            ImSemiProductDEL imInvMasterDEL = null;

            try
            {
                imInvMasterDEL = SelectDataByRecID(dBAccess, strRecId);
                return imInvMasterDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region public bool Insert()
        public bool Insert(IDataEntity iimInvMasterDEL)
        {
            bool bReturn = true;
            ImSemiProductDEL imInvMasterDEL = iimInvMasterDEL as ImSemiProductDEL;
            DBAccess dBAccess = null;

            dBAccess = new DBAccess(false, true);
            try
            {
                bReturn = Insert(dBAccess, imInvMasterDEL);

                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }
            return bReturn;
        }
        public bool Insert(DBAccess dBAccess, ImSemiProductDEL imInvMasterDEL)
        {
            bool bFlag = true;
            ImSemiProductBRL iminvmasterBRL = new ImSemiProductBRL();

            if (iminvmasterBRL.InsertValid(dBAccess, imInvMasterDEL))
            {
                DataFunctions.InsertTable(dBAccess, imInvMasterDAL.Fields, imInvMasterDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #region public bool Delete()
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            bool bFlag = false;

            try
            {
                if (new ImSemiProductBRL ().DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    DataFunctions.DeleteByRecID(dBAccess, "ImSemiProduct", strRecId);

                    dBAccess.CommitTranAndCloseCon();

                    bFlag = true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }

            return bFlag;
        }
        #endregion

        #region public bool Update()
        public bool Update(IDataEntity iImSemiProductDEL)
        {
            bool bReturn = true;
            ImSemiProductDEL imInvMasterDEL = iImSemiProductDEL as ImSemiProductDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, imInvMasterDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }

            return bReturn;
        }
        public bool Update(DBAccess dBAccess, ImSemiProductDEL  imInvMasterDEL)
        {
            bool bFlag = true;
            ImSemiProductBRL imInvMasterBRL = new ImSemiProductBRL();

            if (imInvMasterBRL.UpdateValid(dBAccess, imInvMasterDEL))
            {
                DataFunctions.UpdateTable(dBAccess, imInvMasterDAL.Fields, ImSemiProductDEL.TABLENAME_ImSemiProduct, imInvMasterDEL.Tables[0].Rows[0]);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #endregion
    }
}
